package cn.zkw.controller;

import cn.zkw.service.BobingService;
import cn.zkw.util.BobingUtil;
import cn.zkw.vo.Article;
import cn.zkw.vo.Bobing;
import com.mchange.v1.util.ArrayUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class BobingController {
    @Autowired
    BobingService bobingService;

    @RequestMapping("/bobing/start")
    public String start() {
        return "/activity/bobing/start";
    }

    @RequestMapping("/bobing/index")
    public String index() {
        return "/activity/bobing/index";
    }


    /**
     * 博饼添加接口
     */
    @RequestMapping(value = "/addBobing", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody
    Object addBobing(Bobing bobing, HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        try {
            BobingUtil bobingUtil = new BobingUtil();   //博饼工具类
            //对博饼数据进行处理**********
            String arr1 = request.getParameter("arr1");
//            System.out.println("初始参数:"+arr1);
            String prearrstr[] = arr1.split(",");
            Integer resultarr[] = new Integer[prearrstr.length];
//            System.out.println("接受到请求");
            for (int i = 0; i < prearrstr.length; i++) {
                resultarr[i] = Integer.parseInt(prearrstr[i]);
                resultarr[i] = ++resultarr[i];
            }
//            System.out.println("转换int数组参数:"+ ArrayUtils.toString(resultarr));
            StringBuffer arr = new StringBuffer();
            for (int i = 0; i < resultarr.length; i++) {
                arr.append(resultarr[i]).append(",");
            }
//            System.out.println("stringbuff参数:"+arr);
            arr.deleteCharAt(arr.length() - 1);
            //对博饼数据进行处理**********
            String result = bobingUtil.bobingResult(resultarr); //利用博饼工具类,判断博饼结果
            bobing.setS_score(arr.toString());
            bobing.setS_result(result);
            if (result == null) {
                bobing.setS_result("未中奖");
                jsonObject.put("noResult",true);
            }
            bobing.setS_date(new Date());
            System.out.println("asd" + bobing);
            if (bobing.getS_result() == null) {
                bobing.setS_result("未中奖");
                jsonObject.put("noResult",true);
            }
            if (bobingService.selectByS_numberByCurrentDay(bobing) == null) {
                bobingService.addBobing(bobing);  //添加博饼记录到数据库
            } else {
                jsonObject.put("currentDay", true);  //表示今天已经博过
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        //接口返回数据
        jsonObject.put("result", bobing.getS_result());
        System.out.println(bobing.getS_result());
        if(bobing.getS_result().equals("没中奖")){
            System.out.println("noresout");
            jsonObject.put("noResult",true);
        }
        return jsonObject;
    }
}
